<?php

namespace App\Http\Controllers\Admin;

use App\Facade\AppContext;
use App\Http\Requests\Admin\FindPassRequest;
use App\Http\Requests\Admin\LoginFormRequest;
use App\Http\Requests\Admin\RegisterFormRequest;
use App\Http\Requests\Admin\SendRegisterCodeFormRequest;
use App\Http\Requests\Admin\System\UserSetPasswordFormRequest;
use App\Models\System\Group;
use App\Models\System\Hospital;
use App\Repositories\System\HospitalRepository;
use App\Repositories\System\UserRepository;
use App\Utils\Output;
use Illuminate\Http\Request;

class PublicController extends BaseController
{
    function login(Request $request)
    {
        $uid = AppContext::userId();
        if ($uid > 0) {
//            OperationLog::add([
//                'action' => 2,
//                'origin' => 1,
//                'user_id' => $uid,
//                'remark' => '',
//            ]);

        }
        AppContext::destroy();
        return $this->view('public.login');
    }

    public function lang(Request $request)
    {
        $request->cookie()->put('lang', $request->post('lang'));
        return Output::success();
    }

    public function logging(LoginFormRequest $request, UserRepository $repository): Output
    {
        $result = $repository->login(trim($request->get('username')), $request->get('password'), [Group::PLAT, Group::HOSPITAL, Group::CUSTOM_SERVICE, Group::CALLER, Group::GUIDE]);
        if ($result->isError()) {
            return $result;
        }
        AppContext::login($result->data);
        if ($result->data['group_code'] == Group::PLAT) {
            $hospitals = Hospital::query()->select('id', 'name')->get();
            AppContext::set('hospitals', $hospitals);
            if ($hospitals->count())
                AppContext::hospitalId($hospitals[0]['id']);
        } else {

            if (!empty($result->data['hospital_ids'])) {
                $hospital_ids = explode(',', $result->data['hospital_ids']);
                $hospitals = Hospital::query()->select('id', 'name')->whereIn('id', $hospital_ids)->get();
                AppContext::set('hospitals', $hospitals);
                if ($hospitals->count())
                    AppContext::hospitalId($hospitals[0]['id']);
            } else {
                AppContext::hospitalId(0);
            }
        }
        $url = $request->get('referer');
        $result = Output::data(['referer' => urldecode($url ? $url : admin_url('/index'))]);
        $result->msg = __('system.login.login_success');
        return $result;
    }

    public function register()
    {
        return $this->view('public.register')->with('pageName', '注册');
    }

    public function sendRegisterCode(SendRegisterCodeFormRequest $request, VerificationCodeRepository $repository)
    {
        return $repository->sendRegister($request->get('email'));
    }

    public function postRegister(RegisterFormRequest $request, AgentRepository $memberRepository)
    {
        $email = $request->get('email');
        return $memberRepository->register($email, $request->get('password'));
    }

    public function findPass()
    {
        return $this->view('public.findPass')->with('pageName', '找回密码');
    }

    public function postFindPass(FindPassRequest $request, RetrievePasswordRepository $repository)
    {
        $email = $request->get('email');
        return $repository->send($email);
    }

    public function retrievePassword($code, RetrievePasswordRepository $repository)
    {
        $id = $repository->read($code);
        if ($id) {
            return $this->view('public.retrievePassword')->with('code', $code)->with('pageName', '重置密码');
        }
        return $this->view('error')->with('msg', '');
    }

    public function postRetrievePassword(UserSetPasswordFormRequest $request, RetrievePasswordRepository $repository)
    {
        $password = $request->get('password', 0);
        $code = $request->get('code', 0);
        return $repository->setPassword($code, $password);
    }
}
